﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using LibGreen;
using LibGreen.Events;
using LibGreen.Protocol.IRC.Utility;
using LibGreen.Protocol.IRC.Events;

namespace LibGreen.Protocol.IRC
{
	public struct AliasAndPass{
		public string alias;
		public string pass;
	}

	public class Session : ISessionChatRoom, ISessionConnect, ISessionContact
	{
		#region Port
		public int Port
		{
			get
			{
				return con.Port;
			}
			set
			{
				con.Port = value;
			}
		}

		private const int defaultPort = 6667;
		public int DefaultPort
		{
			get { return 6667; }
		} 
		#endregion
		#region Server
		public string Server
		{
			get
			{
				return con.Server;
			}
			set
			{
				con.Server = value;
			}
		}
		private const string defaultServer = "irc.freenode.net";
		public string DefaultServer
		{
			get { return "irc.freenode.net"; }
		}
		
		#endregion
		private string userName;
		public string UserName
		{
			get { return userName; }
		}
		private string password;
		public string NickName;

		internal Connection con = new Connection(defaultServer, defaultPort);
		
		public Session (string userName, string nickName, string password)
		{
			this.userName = userName;
			this.password = password;
			this.NickName = nickName;
			con.ReadyForLogin += new ReadyForLoginHandler (con_ReadyForLogin);
		}

		void con_ReadyForLogin (Connection con)
		{
			var packetPass = new ClientCommand (Commands.Pass, password);
			var packetNick = new ClientCommand (Commands.Nick, NickName);
			var packetUser = new ClientCommand (Commands.User,
												userName,
												"blankBlank",
												Server,
												"green");
			con.SendMessage (packetPass);
			con.SendMessage (packetNick);
			con.SendMessage (packetUser);
		}

		public event ChatRoomJoinedHandler ChatRoomJoined;
		public event SessionOpenHandler SessionOpen;
		public event SessionFailureHandler SessionFailure;
		public event LoginStatusUpdateHandler LoginStatusUpdate;
		public event ContactStatusUpdatedHandler ContactStatusUpdated;
		public event ContactInfoReceivedHandler ContactInfoReceived;

		public void JoinChatRoom (string channelName, params string[] other)
		{
			throw new NotImplementedException ();
		}

		public void Open ()
		{
			con.Initialize ();
		}

		public void Close ()
		{
			throw new NotImplementedException ();
		}

		

		public void RequestContactInfo (string contactID)
		{
			throw new NotImplementedException ();
		}

	}

}
